package com.wbl.train.business.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wbl.train.business.service.ConfirmOrderService;
import com.wbl.train.common.business.dto.ConfirmOrderDto;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王博纶
 * @since 2024-03-04
 * @description 确认订单
 */
@Slf4j
@RestController
@RequestMapping("/confirm-order")
@Tag(name = "确认订单控制器类",
        description = "ConfirmOrderController 确认订单 后端数据接口")
public class ConfirmOrderController {
    @Autowired
    private ConfirmOrderService confirmOrderService;

    /**
     * 新增 确认订单
     * @param confirmOrderDto 确认订单Dto
     * @return Result
     */
    @SentinelResource(value = "confirmOrderDo",blockHandler = "doConfirmOrderBlock")
    @Operation(summary = "新增确认订单", description = "新增确认订单")
    @PostMapping("/do")
    public R<Boolean> doConfirm(@Valid @RequestBody ConfirmOrderDto confirmOrderDto) {
        confirmOrderService.beforeConfirmOrder(confirmOrderDto);
        return R.ok("确认订单完成",true);
    }

    /**
     * 被sentinel限流降级的处理发放
     * @param confirmOrderDto 订单确认的参数
     * @param e 降级的异常参数
     */
    public void doConfirmOrderBlock(ConfirmOrderDto confirmOrderDto, BlockException e){
        log.info("购票被限流：{}",confirmOrderDto);
        throw new ApplicationException(ResultCode.SENTINEL_DEAL_BUY_TICKET,"购票方法被sentinel限流了，请稍后在尝试");
    }
}
